#region license
# This file is part of Grawlix.
# 
# Grawlix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# Grawlix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with Grawlix.  If not, see
# <http://www.gnu.org/licenses/>.
#endregion

"""An abstraction to allow dynamically adding pipeline steps."""

namespace Grawlix.Meta
import Boo.Lang.Compiler
import System

transient class InnerPipeline(CompilerPipeline):
"""An extra pipeline that can be added during the compile."""
	
	def constructor(context as CompilerContext, trigger as Type):
	"""
	Construct a pipeline run inbetween normal steps.
	Param context: The compiler context on which to add this pipeline.
	Param trigger: The step which triggers the pipeline.
	"""
		_context = context
		context.Parameters.Pipeline.AfterStep += StepResponse
		_trigger = trigger
		
	private def StepResponse(sender, args as CompilerStepEventArgs):
	"""
	Run the pipeline.
	Param sender: The sender of the event.
	Param args: The event arguments.
	"""
		if _trigger.IsInstanceOfType(args.Step):
			Run(args.Context)
	
	Before:
	"""Whether this step happens before the pipeline."""
		get:
			return _before
		set:
			if value == _before:
				return
			_before = value
			if value:
				_context.Parameters.Pipeline.BeforeStep += StepResponse
				_context.Parameters.Pipeline.AfterStep -= StepResponse
			else:
				_context.Parameters.Pipeline.BeforeStep -= StepResponse
				_context.Parameters.Pipeline.AfterStep += StepResponse
	
	private _before as bool
	"""Whether this step happens before the pipeline."""

	private _trigger as Type
	"""The step which triggers this pipeline."""
	
	private _context as CompilerContext
	"""The context on which to execute."""
	
	